<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.roleName"
          placeholder="角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          @click="
            dialogFormVisible = true;
            sysRole={};
          "
          >角色添加</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="delBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 角色列表 -->
    <el-table :data="roleList" style="width: 100%"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="roleCode" label="角色id"></el-table-column>
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
      <el-table-column prop="roleType" label="角色类型">
          <template slot-scope="scope">
            <div>{{ scope.row.roleType == 1 ? "业务角色" : "管理角色" }}</div>
          </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row);"
              >修改</el-button
            >
          <el-button
            type="warning"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="searchMap.page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pages"
    >
    </el-pagination>
    <!-- 添加修改弹出页 -->
    <el-dialog
      :title="sysRole.roleCode? '角色修改' : '角色添加'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="sysRole">
        <el-form-item label="角色名称" label-width="100px">
          <el-input v-model="sysRole.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" label-width="100px">
          <el-input v-model="sysRole.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" label-width="100px">
          <el-select v-model="sysRole.roleType" placeholder="角色类型">
            <el-option label="业务角色" value="1"></el-option>
            <el-option label="管理角色" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { search, save, delOne, delByIds } from "@/api/role.js";
export default {
  data() {
    return {
      dialogFormVisible: false, //添加弹窗开关
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      roleList: [],
      pages: 0,
      sysRole: {},
      delData: {}, //删除的信息
      delAllData: {}, //批量删除数据
      role: {}, //角色信息
    };
  },
  methods: {
    // 查询
    search() {
      search(this.searchMap).then((res) => {
        this.roleList = res.data;
        this.pages = res.total;
      });
    },
    //添加
    save() {

      save(this.sysRole).then((res) => {
        if (res.success) {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
          this.search();
          this.dialogFormVisible = false; //添加弹窗开关
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
      })
    },
    //删除
    handleDelete(index, data) {
      this.delData = data;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delOne(this.delData).then((res) => {
          if (res.success) {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            this.search();
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    
    },
    //批量删除
    handleSelectionChange(val) {
      this.delAllData = val;
    },
    delBatch(){
    var roleCodes = [];
      this.delAllData.forEach((item) => {
        roleCodes.push(item.roleCode);
      }); 
      console.log(roleCodes)
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delByIds(roleCodes).then((res) => {
          if (res.success) {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            this.search()
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
            this.search()
          }
        });
      });
    
    },
    //修改
    handleEdit(index,row){
      console.log(row)
      this.dialogFormVisible = true;
      this.sysRole = row;
      this.sysRole.roleCode=row.roleCode
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.search();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.search();
    },
  },
  //页面加载时显示数据
  created() {
    this.search();
  },
};
</script>